package xiaoa.java.log;

import java.util.LinkedHashMap;


import org.apache.log4j.Logger;


import com.alibaba.fastjson.JSON;

/**
 *  日志打印类
 * @author lkc
 *
 */
public class L  extends LogUtils{

	/**
	 * 创建一个logger
	 */
	private  static Logger    l  = Logger.getLogger("l");
	
	
	public static void setL(Logger l) {
		L.l = l;
	}


	public static void info( long startTime , long endTime ,Object  os){
		info(os , "useTime : " + (endTime - startTime));
	}

	
	public static void showUsedTime(String fun,LinkedHashMap<String, Object>  logMap ,long startTime , long endTime ){
		info(fun + ":" ,JSON.toJSON(logMap) , "useTime : " + (endTime - startTime));
	}
	
	
	/**
	 * info
	 * @Title: info
	 * @param os
	 * @author xiaoa
	 */
	public static void info(Object...  os){
		
		// 距离上次时将
		
		// 获取线程名字
		String threadName = Thread.currentThread().getName();
		
		String tid = getTransactionIdThreadLocal();
		
		l.info( "threadName = " + threadName + (tid == null ? "" : "tid = " + tid) + ":"  + merge(os));

	}
	
	
	/**
	 * debug
	 * @Title: debug
	 * @param os
	 * @author xiaoa
	 */
	public static void debug(Object...  os){
		l.debug(merge(os));
	}
	
	
	/**
	 * 将对象合并为字符串
	 * @Title: merge
	 * @param os
	 * @return
	 * @author xiaoa
	 */
	private static String merge(Object...  os){
		
		if(os == null ){
			return null;
		}
		
		
		StringBuilder  sb = new StringBuilder();
		
		for (Object o : os){
			
			if (o == null){
				continue;
			}
			
			if (o instanceof CharSequence){
				sb.append(o.toString().replace("\n",""));
			}else {
				sb.append(JSON.toJSONString(o));
			}
			
		}
		
		return sb.toString();
	} 
	
	
}
